/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.ResourceBundle;
import mx.edu.itschapala.sistemas.septimo.control.ClientelnLocal;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.control.VentaLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.modelo.Venta;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVista;


/**
 *
 * @author Usuario
 */
public class VentaCrearEditar extends javax.swing.JFrame {
    
Calendar calendario = Calendar.getInstance();
int hora, minutos, segundos;
int dia,mes,año;
String fecha;
String horaVenta;
int idContador;

  private ProductoLnLocal productoControl;
     private VentaLnLocal ventaControl;
    private ClientelnLocal ClienteControl;
    private TipoVista tipoVentana;
    
    

private Venta venta   ;
 List<Cliente> tmpC = new ArrayList<Cliente>();
  List<Producto> tmpP = new ArrayList<Producto>();
   
    public VentaCrearEditar(Venta venta,ProductoLnLocal  productoControl,ClientelnLocal ClienteControl,VentaLnLocal ventaControl,TipoVista tipoVentana) {
       this.ventaControl=ventaControl;
       this.productoControl=productoControl;
       this.ClienteControl=ClienteControl;
       if(ventaControl.getTodos().size()==0){
       idContador=1;
       }
 else{
       idContador=ventaControl.getTodos().size();
       }
       
       // this.ClienteControl=ClienteControl;
       this.venta=venta;
       this.tipoVentana=tipoVentana; 
        initComponents();
             this.setResizable(false);
        
             switch (tipoVentana) {
            case VENTANA_REGISTRO:
    jTextField1.setEditable(false);
     jTextField1.setText(idContador+"");
                jLabel6.setText("NUEVA VENTA");
                        jComboBox1.removeAllItems();   
                  tmpC =ClienteControl.getTodos();
                  

          for (Cliente c :tmpC ) {
           
                  jComboBox1.addItem( c.getNombre());
           } 
          //2 jcombo
          
               jComboBox2.removeAllItems();   
                  tmpP =productoControl.getTodos();

          for (Producto p :tmpP ) {
           
                  jComboBox2.addItem( p.getNombre());
                  
           } 
          
          
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR POR ID");
              
               
                jButton2.setVisible(false);
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("MODIFICAR VENTA");
                jTextField1.setEditable(false); 
                  jTextField1.setText(venta.getId()+"");
              
              
                break;
            case VENTANA_ELIMINAR:
                  jLabel6.setText("ELIMINAR VENTA");
                    jTextField1.setText(venta.getId()+"");
              
               
                break;
        }
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("Venta");

        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setLayout(new java.awt.GridLayout(5, 0));

        jLabel1.setText("                                       id");
        jPanel1.add(jLabel1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });
        jPanel1.add(jTextField1);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel7.setText("cliente");

        jLabel8.setText("Producto");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(247, 247, 247)
                .addComponent(jLabel6)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel7)
                        .addGap(30, 30, 30)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel6)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addComponent(jButton2))))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        switch (tipoVentana) {
            
            case VENTANA_REGISTRO:
             
                hora =calendario.get(Calendar.HOUR_OF_DAY);
minutos = calendario.get(Calendar.MINUTE);
segundos = calendario.get(Calendar.SECOND);
this.horaVenta=hora+":"+minutos+":"+segundos;
//fecha
dia=calendario.get(Calendar.DAY_OF_MONTH);
mes=calendario.get(Calendar.MONTH);
año=calendario.get(Calendar.YEAR);
this.fecha=dia+"/"+mes+"/"+año;


         

        
                Venta v = new Venta();
            v.setFecha(fecha);
            jTextField1.setText(idContador+"");
           
            v.setId(Integer.parseInt(jTextField1.getText()));
            v.setCliente(jComboBox1.getSelectedItem()+"");
//             for (Producto p :tmpP ) {
//            if(jComboBox2.getSelectedItem()+""==p.getNombre()){
//            jComboBox2.addItem( p.getNombre());
//            }
//                  
//                  
//           } 
             System.out.println(jComboBox2.getSelectedItem().toString());
           
    Producto pj  =  productoControl.buscarPorNombre(jComboBox2.getSelectedItem().toString());
                
            v.setProducto(pj);
            
            v.setHora(horaVenta);
          ventaControl.registrar(v);
                System.out.println(ventaControl.getTodos().size());
           idContador=ventaControl.getTodos().size();
            jTextField1.setText(idContador+"");
           
         
            break;
            case VENTANA_BUSQUEDA:
            this.dispose();
            break;
            case VENTANA_MODIFICAR:

            v = new Venta();
            v.setFecha(fecha);
            v.setId(Integer.parseInt(jTextField1.getText()));
           
           // ventaControl.modificar(v);
            jTextField1.setText("");
          
         
            break;
            case VENTANA_ELIMINAR:
            v = new Venta();
            v.setId(Integer.parseInt(jTextField1.getText()));
           // ventaControl.eliminar(v);
            jTextField1.setText("");
       

            break;

        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        if (tipoVentana != TipoVista.VENTANA_REGISTRO) {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {

                Venta v = ventaControl.buscarPorCodigo(Integer.parseInt(jTextField1.getText()));
                if (v != null) {
                   
                   
                    if (tipoVentana == TipoVista.VENTANA_MODIFICAR) {
                        
                    

                    }
                }
            }
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}

